<?php
namespace App\Models\AppModels;
use App\Models\ParentModel;
use DB;
/**
 * 抖音处理视频表model
 * @author puppet
 */
class Auth extends ParentModel{
	/**
	* 与模型关联的数据表。
	*
	* @var string
	*/
	protected $table = 'pt_dy_access_token';

	/**
	* DB::直接操作的数据库表名
	* @var string
	*/
	protected $db_table = 'pt_dy_access_token';

	/**
	* 主键 默认id。
	*
	* @var string
	*/
	protected $primaryKey = 'id';

	/**
	* 指定是否模型应该被戳记时间。
	*
	* @var bool
	*/
	public $timestamps = false;
	//查找是否存在用户
    function checkUserinfo($id){
        $re=DB::select("select id from pt_partner where status=1 and id=?",[$id]);

        return isset($re[0]['id']) ? $re : false;
    }
	//查找需要添加内容关键词的用户
	function selUserList(){
		$re=DB::select("select id from pt_partner where status=1 and type=?",[2]);
		return isset($re[0]['id']) ? $re : false;
	}
	//插入内容关键词
	function addKeyword($data){
		DB::table("pt_keyword_conent")->insertGetId($data);
	}
	
	//接口日志记录
	function api_msg($data){
		DB::table("pt_api_msg")->insertGetId($data);
	}
	
	//评论入库
    function saveVideoComment($data, $video_id, $comment_id,$partner_id,$type){
        $sql = "select id from pt_user_info_comment where video_id = {$video_id} and comment_id = {$comment_id} and type={$type} and partner_id= ?";
        $re = DB::select($sql, [$partner_id]);
        $id = isset($re[0]) ? $re[0]['id'] : false;
        if (!$id) {
            $data['video_id'] = $video_id;
            $data['comment_id'] = $comment_id;
			$data['partner_id'] = $partner_id;
			$data['type'] = $type;
            DB::table("pt_user_info_comment")->insertGetId($data);
        }
    }
    //查对应的accesstoken
    function getaccesstoken($open_id){
        $sql="select access_token from pt_dy_access_token where status = ? and open_id='$open_id' ";
        $re=DB::select($sql,[1]);
        return isset($re[0]['access_token']) ? $re : false;
    }
    //查询内容关键词列表
    function getKeyComments($user_id){
        $sql="select name from pt_keyword_conent where status = ? and user_id={$user_id} group by name";
        $re=DB::select($sql,[1]);
        return isset($re[0]['name']) ? $re : false;
    }
	
	//查询用户是否存在
	function checkUser($partner_id){
		$sql="select id from pt_partner where type=2 and status=? and id={$partner_id}";
		$re=DB::select($sql,[1]);
		return isset($re[0]['id']) ? true : false;
	}
	
	//查询询盘套餐数量是否上限
	function checkInq($partner_id){
		$re=DB::select("select count(id) as count from pt_partner_comment where partner_id=?",[$partner_id]);
		$re=isset($re[0]['count']) ? $re[0]['count'] : 0;
		
		$reTwo=DB::select("select count(id) as count from pt_user_info_comment where partner_id=?",[$partner_id]);
		$reTwo=isset($reTwo[0]['count']) ? $reTwo[0]['count'] : 0;
		
		$reThree=DB::select("select num2 from pt_partner_meal where partner_id=?",[$partner_id]);
		$reThree=isset($reThree[0]['num2']) ? $reThree[0]['num2'] : 0;
		
		return $reThree-$reTwo-$re > 0 ? true : false;
	}
	
	
	
	
	//定时任务 主页抓取指定数当天最新视频  评论
	function selUserInfo(){
		$sql="select a.id,a.number_id,a.partner_id,b.meal_name from pt_user_info as a";
		$sql.=" left join pt_partner_meal as b on b.partner_id=a.partner_id";
		$sql.=" left join pt_partner as c on c.id=a.partner_id";
		$sql.=" where a.system_type=0 and a.info_type=1 and a.status=? and b.end>='".date('Y-m-d')."' and c.status=1";
		$re=DB::select($sql,[1]);
		return isset($re[0]['id']) ? $re : false;
	}
	//获取增加的那个主页账户
	function getNewUserInfo($id){
        $sql="select a.id,a.number_id,a.partner_id,b.meal_name from pt_user_info as a";
        $sql.=" left join pt_partner_meal as b on b.partner_id=a.partner_id";
        $sql.=" left join pt_partner as c on c.id=a.partner_id";
        $sql.=" where a.system_type=0 and a.info_type=1 and a.id=$id and a.status=1 and b.end>='".date('Y-m-d')."' and c.status=1";
        $re=DB::select($sql);
        return isset($re[0]['id']) ? $re : false;
    }
}